<?php
namespace MyClass;
class Caigou extends \MyClass\Common {
    //招标列表
    public function Show(){
			$condition = " 1=1";
			if($_REQUEST['city']) {
				$condition.=" AND inquirysheet.City = ".$_REQUEST['city'];
				$this->s('cityid',$_REQUEST['city']);
			}
			if($_REQUEST['cityname']){
				$this->s('cityname',$_REQUEST['cityname']);
			}
			if($_REQUEST['order']){
				if($_REQUEST['order'] ==1) {
					$ordertype = 'inquirysheet.Time desc';
				} else{
					$ordertype = 'inquirysheet.Time asc';
				}
				$this->s("order",$_REQUEST['order']);
			}else{
				$ordertype = 'inquirysheet.Time desc';
			}
			if($_REQUEST['st'] ==2) {//已经结束()
				$condition.= " AND inquirysheet.Status=2 ";
			}elseif($_REQUEST['st'] ==4){
			
				$condition.= " AND inquirysheet.Status=4";
			} elseif($_REQUEST['st'] ==1){//招标中
				$condition.= " AND inquirysheet.Status=1";
			}else{
				//$condition.= " AND inquirysheet.Status in(1,2,3)";
				$condition.= " AND inquirysheet.Status in(1,2,4)";
			}
	if(!$_SESSION['UserInfo']['id']){//未登录
			$condition.= " AND inquirysheet.Zbtype=1";
			//$p = new \Extend\Page($this->m('inquirysheet')->where($condition)->count(),15);
			$list = $this->m('inquirysheet')
				->join('company on inquirysheet.Cid=company.Id')
				->where($condition)
				->field('inquirysheet.*,company.CompanyName,company.CompanyAddress')
				->order("Time desc")
				->limit(6)
				->order($ordertype)
				->select();
			
			$this->s('list',$list);
		
			

	}else{//已登录
			if(!isset($_SESSION['UserInfo']['cid'])){
				$condition.= " AND inquirysheet.Zbtype=1";
				//$p = new \Extend\Page($this->m('inquirysheet')->where($condition)->count(),15);
				$list = $this->m('inquirysheet')
					->join('company on inquirysheet.Cid=company.Id')
					->where($condition)
					->field('inquirysheet.*,company.CompanyName,company.CompanyAddress')
					->order("Time desc")
					->limit(6)
					->order($ordertype)
					->select();
			}else{
				$bus = $this->m("yaobiao")->where("Syid = ".$_SESSION['UserInfo']['cid'])->select();
			
				foreach($bus as $key=>$vo){
					$bgscid[] = $vo['Iid'];
				}
				/*$bus = $this->m('business')->where("Cid = ".$_SESSION['UserInfo']['cid'])->select();//我关注的
				$gus = $this->m('business')->where("Gid = ".$_SESSION['UserInfo']['cid'])->select();//关注我的
				foreach($bus as $key=>$vo){
						foreach($gus as $k=>$v){
							if($vo['Gid'] == $v['Cid']){
								//$bgs[] = $v['Id'];	
								$bgscid[] = $v['Cid'];//获取相互收藏数据的Cid
							}
						}
				}*/
				if($bgscid>0){
					$bgscid = join(",",$bgscid);

				}else{
					$bgscid = 99999999999999;//暂无此数;
				}
				//$p = new \Extend\Page($this->m('inquirysheet')->where($condition." AND ((inquirysheet.Id in(".$bgscid.") And inquirysheet.Zbtype = 2) OR (inquirysheet.Cid = ".$_SESSION['UserInfo']['cid']." AND inquirysheet.Zbtype = 2)  OR inquirysheet.Zbtype = 1)")->count(),15);
				//下面可以看到他本人的邀标和商友的邀标以及所有的公共投标 inquirysheet.Cid in(".$bgscid.") And inquirysheet.Zbtype = 2 //商友发的邀标
				$list = $this->m("inquirysheet")
					->join("company on inquirysheet.Cid=company.Id")
					->where($condition." AND ((inquirysheet.Id in(".$bgscid.") And inquirysheet.Zbtype = 2) OR (inquirysheet.Cid = ".$_SESSION['UserInfo']['cid']." AND inquirysheet.Zbtype = 2)  OR inquirysheet.Zbtype = 1)")
					->field('inquirysheet.*,company.CompanyName,company.CompanyAddress')
					->order("Time desc")
					->limit(6)
					->order($ordertype)
					->select();
			}

			$this->s('list',$list);
		}
       
        $this->v();
	}
    //详情
    public function Info(){
        if(!$_REQUEST['Id']) {
            $this->error('参数错误!');
        }
		$info = $this->m("inquirysheet")
				->join("company on inquirysheet.Cid = company.Id")
				->where("inquirysheet.Id=".$_REQUEST['Id'])
				->field("inquirysheet.*,company.CompanyName,company.CompanyAddress")->find();
		if(isset($_SESSION['UserInfo']['cid'])){
			$state = $this->m('baojia')->where("Iid=".$_REQUEST['Id']." AND Cid=".$_SESSION['UserInfo']['cid'])->find();
		}
		if($state) {
			$state = 1;//判断是否已经招标
		} else {
			$state = 0;
		}
		
		if($_SESSION['UserInfo']['id']){
			if($_SESSION['UserInfo']['cid']){
				$shcomp = $this->m("company")->field("Status")->where("Id=".$_SESSION['UserInfo']['cid'])->find();
				if($_SESSION['UserInfo']['cid'] == $info['Cid']){
					$state = 2;//不能招标自身
				}
			}
			$this->s("shcomp",$shcomp);
		}
		if(!$_SESSION['UserInfo']['id'] && $info['Zbtype'] == 2){//未登录并且是邀标
			$this->error("请登录查看",T('Common/Login'));
		}
		/*查看是公开招标还是邀标 如果为邀标并且不是自己的*/
		if(isset($_SESSION['UserInfo']['cid'])){
			
			if($_SESSION['UserInfo']['cid']!=$info['Cid'] && $info['Zbtype'] == 2){
				$yb = $this->m('yaobiao')->where("Iid = ".$_REQUEST['Id']." AND Syid = ".$_SESSION['UserInfo']['cid'])->find();
				if(!$yb){//如果不存在则不属于邀标
					$state = 3;	
				}
			}
		}
		

		if($info['Status'] == 2){//如果招标已经下单 获取中标商家
			$zbproducts = $this->m("baojia")->where("Iid = ".$info['Id']." AND Status = 1")->find();
			$this->s("zbproducts",$zbproducts);
		}
		$info['file'] = explode("/",trim($info['file'],"/"));
		$info['file'] = join(",",$info['file']);
		$sedbid =  $this->m('inquirysheet')->where(array('Status'=> 2))->limit(10)->select();
		$this->s('sedbid',$sedbid);
        $this->s('info',$info);
        $this->s('state',$state);
        $this->v();
    }
    //添加报价
    //详情
    public function Add(){
	
        $save['Iid'] = $_REQUEST['Iid'];
        $save['Cid'] = $_REQUEST['Cid'];
        $save['Amount'] = $_REQUEST['total'];
        $save['Company'] = $_REQUEST['com'];
        $save['Phone'] = $_REQUEST['tel'];
        $save['Date'] = time();
        $save['File'] = $_REQUEST['files'][0];
		//$save['File']  = join("/",$_REQUEST['files']);
		//$save['File'] = $save['File'] ."/";
        $result = $this->m('baojia')->add($save);
        if($result) {
           $this->success('投标成功', T('Caigou/Show'));
        } else {
            $this->error('系统繁忙');
        }
    }
	//ajax预加载
	public function PageAjax(){
		if(!isset($_REQUEST['page'])){
				$this->error("非法操作");
		}
		$thispage = (int)$_REQUEST['page']*6;
		//$_REQUEST['page'] = 1;
		$condition = " 1=1";
			if($_REQUEST['city']) {
				$condition.=" AND inquirysheet.City = ".$_REQUEST['city'];
			}
			if($_REQUEST['order'] ==1 || !$_REQUEST['order']) {
				$ordertype = 'inquirysheet.Time desc';
			} elseif ($_REQUEST['order'] ==2) {
				$ordertype = 'inquirysheet.Time asc';
			}
			if($_REQUEST['st'] ==3){//已到期
				$condition.= " AND inquirysheet.Status=1";
				$condition.= " AND (inquirysheet.EndTime+86400) <= ".time();

			}elseif($_REQUEST['st'] ==2) {//已经结束
				$condition.= " AND inquirysheet.Status=2";
			} elseif($_REQUEST['st'] ==1){//招标中
				$condition.= " AND inquirysheet.Status=1";
				$condition.= " AND (inquirysheet.EndTime+86400) > ".time();
			}else{
				$condition.= " AND inquirysheet.Status in(1,2,3)";
		}

		if(!$_SESSION['UserInfo']['id']){//未登录
			$condition.= " AND inquirysheet.Zbtype=1";
			//$p = new \Extend\Page($this->m('inquirysheet')->where($condition)->count(),15);
			$count = $this->m('inquirysheet')->where($condition)->count();
			$list = $this->m('inquirysheet')
				->join('company on inquirysheet.Cid=company.Id')
				->where($condition)
				->field('inquirysheet.*,company.CompanyName,company.CompanyAddress')
				->order("Time desc")
				->limit($thispage.", 6")
				->order($ordertype)
				->select();

	}else{//已登录
			if(!isset($_SESSION['UserInfo']['cid'])){
				$this->error("请完善公司信息信息");
			}
			$bus = $this->m("yaobiao")->where("Syid = ".$_SESSION['UserInfo']['cid'])->select();
		
			foreach($bus as $key=>$vo){
				$bgscid[] = $vo['Iid'];
			}
			/*$bus = $this->m('business')->where("Cid = ".$_SESSION['UserInfo']['cid'])->select();//我关注的
			$gus = $this->m('business')->where("Gid = ".$_SESSION['UserInfo']['cid'])->select();//关注我的
			foreach($bus as $key=>$vo){
					foreach($gus as $k=>$v){
						if($vo['Gid'] == $v['Cid']){
							//$bgs[] = $v['Id'];	
							$bgscid[] = $v['Cid'];//获取相互收藏数据的Cid
						}
					}
			}*/
			if($bgscid>0){
				$bgscid = join(",",$bgscid);

			}else{
				$bgscid = 99999999999999;//暂无此数;
			}
			$count =$this->m('inquirysheet')->where($condition." AND ((inquirysheet.Id in(".$bgscid.") And inquirysheet.Zbtype = 2) OR (inquirysheet.Cid = ".$_SESSION['UserInfo']['cid']." AND inquirysheet.Zbtype = 2)  OR inquirysheet.Zbtype = 1)")->count(); 
			$list = $this->m("inquirysheet")
				->join("company on inquirysheet.Cid=company.Id")
				->where($condition." AND ((inquirysheet.Id in(".$bgscid.") And inquirysheet.Zbtype = 2) OR (inquirysheet.Cid = ".$_SESSION['UserInfo']['cid']." AND inquirysheet.Zbtype = 2)  OR inquirysheet.Zbtype = 1)")
				->field('inquirysheet.*,company.CompanyName,company.CompanyAddress')
				->order("Time desc")
				->limit($thispage.", 6")
				->order($ordertype)
				->select();
		}

		foreach($list as $key=>$v){
			if($v['Status']==1 && ($v[EndTime]+86400) > time()){
				$list[$key]['ifjs'] = 1;
			}
			if($v['Status']==2){
				$list[$key]['ifjs'] = 2;
			}
			if($v['Status']==1 && ($v[EndTime]+86400) <= time()){
				$list[$key]['ifjs'] = 3;
			}
			$list[$key]['Time'] = date("Y-m-d",$v['Time']);
		}
		
		$ifhy = ceil($count/6);
		if((int)$_REQUEST['page']>$ifhy){
			$list['ifhy'] = 1;
		}
		echo json_encode($list);
	
	}
}